package com.devunion.reader.client.panel;

import com.devunion.reader.client.command.ProjectTreeInfoCmd;
import com.devunion.reader.client.core.AbstractComposite;
import com.devunion.reader.client.core.Refreshable;
import com.devunion.reader.client.resources.MessagesProperties;
import com.devunion.reader.client.to.ProjectTreeTo;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binder.TreeBinder;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.StoreFilterField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.tree.Tree;

/**
 * @author Timoshenko Alexander 04.07.2009 : 18:59:12
 */
public class TreeProjectPanel extends AbstractComposite implements Refreshable {
    private TreeLoader<ProjectTreeTo> loader;
    private TreeBinder binder;
    private ProjectTreeInfoCmd cmd = new ProjectTreeInfoCmd();

    public ProjectTreeInfoCmd getCmd() {
        return cmd;
    }

    public void initialize() {
        setLayout(new FitLayout());
        setFrame(true);
        setScrollMode(Style.Scroll.AUTO);
        setButtonAlign(Style.HorizontalAlignment.LEFT);


        loader = new BaseTreeLoader<ProjectTreeTo>(cmd) {
            @Override
            public boolean hasChildren(ProjectTreeTo projectTreeTo) {
                return projectTreeTo.hasChildren() != null && projectTreeTo.hasChildren();
            }
        };

        TreeStore<ProjectTreeTo> store = new
                TreeStore<ProjectTreeTo>(loader);
        Tree tree = new Tree();
        binder = new TreeBinder(tree, store);
        binder.setDisplayProperty(ProjectTreeTo.NAME);
        binder.setStringProvider(new ModelStringProvider<ProjectTreeTo>() {
            public String getStringValue(ProjectTreeTo projectTreeTo, String s) {
                if (projectTreeTo.hasChildren() == null) {
                    return MessagesProperties.INSTANCE.getpagenametemplate(String.valueOf(projectTreeTo.getPageNumber()));
                }
                return projectTreeTo.get(s);
            }
        });

        StoreFilterField<ProjectTreeTo> filter = new StoreFilterField<ProjectTreeTo>() {
            protected boolean doSelect(Store<ProjectTreeTo> projectTreeToStore, ProjectTreeTo projectTreeTo,
                                       ProjectTreeTo projectTreeTo1, String s, String s1) {
                if (projectTreeTo1 != null && projectTreeTo1.hasChildren() != null && projectTreeTo1.hasChildren()) {
                    String name = projectTreeTo1.getName().toLowerCase();
                    return name.contains(s1);
                }

                return false;

            }
        };
        /*filter.setWidth("100%");
        filter.bind(store);
        setTopComponent(filter);*/

        add(tree);        
    }

    public void refresh() {
        loader.load(null);
    }

    public TreeBinder getBinder() {
        return binder;
    }
}
